package home.study.xxy.bs.service.impl;

import home.study.xxy.bs.entity.ComJob;
import home.study.xxy.bs.entity.Page;
import home.study.xxy.bs.entity.ResultList;
import home.study.xxy.bs.mapper.BillMapper;
import home.study.xxy.bs.mapper.JobMapper;
import home.study.xxy.bs.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: JobServiceImpl
 * @功能: 任务管理实现层
 * @Author Xiao Xiangyu
 * @Date: 2021/4/18 15:39
 * @Version 1.0
 */
@Slf4j
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private BillMapper billMapper;

    /**
     * 查询所有任务
     * */
    @Override
    public ResultList<ComJob> queryAllJob(ComJob comJob,Page pageVo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", pageVo.getIndex());
        params.put("size", pageVo.getPageSize());
        int total = jobMapper.queryJobCount(comJob);
        List<ComJob> comJobList= jobMapper.queryAll(comJob,params);;
        ResultList<ComJob> jobList = new ResultList<ComJob>();
        jobList.setList(comJobList);
        jobList.setTotal(total);
        return jobList;
    }
    /**
     * 查询每个用户接的任务
     * */
    @Override
    public List queryUserJobCount() {
        List stringObjectMap = jobMapper.queryUserJobCount();
        return stringObjectMap;
    }
    /**
     * 根据id查询用户接的任务
     * */
    @Override
    public int queryJobCountById(ComJob comJob) {
        int i = jobMapper.queryJobCountById(comJob);
        return i;
    }

    /**
     * 通过任务id删除任务
     * @param jobId
     * @return
     */
    @Override
    public int deleteJob(String jobId) {
        int i = jobMapper.deleteJob(jobId);
        return 0;
    }

    /**
     * 修改任务状态
     * */
    @Override
    public int changeJobState(ComJob comJob) {
        int updateJobState = jobMapper.changeJobState(comJob);
        if ("3".equals(comJob.getJobState())){
            billMapper.changeBill(comJob);
        }
        return updateJobState;
    }

//    /**
//     * 根据用户id查询所有任务
//     *
//     * @return 查询结果和总数
//     * */
//    @Override
//    public ResultList<ComJob> queryJobById(ComJob comJob, Page pageVo, Date startTime, Date endTime) {
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("page", pageVo.getIndex());
//        params.put("size", pageVo.getPageSize());
//        int total = jobMapper.queryJobByIdCount(comJob,startTime,endTime);
//        List<ComJob> comJobList= jobMapper.queryJobById(comJob,params,startTime,endTime);
//        ResultList<ComJob> jobList = new ResultList<ComJob>();
//        jobList.setList(comJobList);
//        jobList.setTotal(total);
//        return jobList;
//    }
    @Override
    public ResultList<ComJob> queryJobById(ComJob comJob, Page pageVo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", pageVo.getIndex());
        params.put("size", pageVo.getPageSize());
        int total = jobMapper.queryJobByIdCount(comJob);
        List<ComJob> comJobList= jobMapper.queryJobById(comJob,params);
        ResultList<ComJob> jobList = new ResultList<ComJob>();
        jobList.setList(comJobList);
        jobList.setTotal(total);
        return jobList;
    }
    /**
     * 根据接任务id查询所有任务
     *
     * @return 查询结果和总数
     * */
    @Override
    public ResultList<ComJob> queryJobByStaffId(ComJob comJob,Page pageVo) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", pageVo.getIndex());
        params.put("size", pageVo.getPageSize());
        int total = jobMapper.queryJobByIdCount(comJob);
        List<ComJob> comJobList= jobMapper.queryJobByStaffId(comJob,params);
        ResultList<ComJob> jobList = new ResultList<ComJob>();
        jobList.setList(comJobList);
        jobList.setTotal(total);
        return jobList;
    }

    /**
     * 添加任务
     * 成功时添加账单
     * */
    @Override
    public void addJob(ComJob comJob) {
        int i = jobMapper.addJob(comJob);
        if (i==1){
            billMapper.addBill(comJob);
        }else{
            log.error("账单生成失败");
        }
    }

}
